package com.ruoyi.web.controller.system;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;


import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.system.domain.SecurityActivity;
import com.ruoyi.system.service.ISecurityActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SecurityReport;
import com.ruoyi.system.service.ISecurityReportService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 安防上报
 * Controller
 *
 * @author ruoyi
 * @date 2021-07-22
 */
@RestController
@RequestMapping("/system/report")
public class SecurityReportController extends BaseController {
    @Autowired
    private ISecurityReportService securityReportService;
    @Autowired
    private ISecurityActivityService securityActivityService;

    /**
     * 查询安防上报
     * 列表
     */
//    @PreAuthorize("@ss.hasPermi('system:report:list')")
    @GetMapping("/list")
    public TableDataInfo list(SecurityReport securityReport) {
        startPage();
        List<SecurityReport> list = securityReportService.selectSecurityReportList(securityReport);
        return getDataTable(list);
    }

    /**
     * 导出安防上报
     * 列表
     */
//    @PreAuthorize("@ss.hasPermi('system:report:export')")
    @Log(title = "安防上报", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SecurityReport securityReport) {
        List<SecurityReport> list = securityReportService.selectSecurityReportList(securityReport);
        ExcelUtil<SecurityReport> util = new ExcelUtil<SecurityReport>(SecurityReport.class);
        return util.exportExcel(list, "report");
    }

    /**
     * 获取安防上报
     * 详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:report:query')")
    @GetMapping(value = "/{srId}")
    public AjaxResult getInfo(@PathVariable("srId") Long srId) {
        return AjaxResult.success(securityReportService.selectSecurityReportById(srId));
    }

    /**
     * 新增安防上报
     */
//    @PreAuthorize("@ss.hasPermi('system:report:add')")
    @Log(title = "安防上报", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SecurityReport securityReport) {
        securityReportService.insertSecurityReport(securityReport);
        return AjaxResult.success(securityReport);
    }

    @Log(title = "安防上报", businessType = BusinessType.INSERT)
    @PostMapping("/upload")
    public AjaxResult add(Long srId, MultipartFile file, MultipartFile video) {
        SecurityActivity securityActivity = new SecurityActivity();
        try {
            if (null != file) {
                //若依默认系统上传下载路径
                String profile = RuoYiConfig.getProfile();
                byte[] bytes = file.getBytes();
                //文件夹
                Path path2 = Paths.get(profile + "//srId//" + srId);
                //具体文件
                Path path = Paths.get(profile + "//srId//" + srId + "//" + file.getOriginalFilename());
                File file1 = path.toFile();
                //创建文件夹
                if (!file1.exists()) {
                    path2.toFile().mkdirs();
                }
                if (file1.isFile()) {
                    return AjaxResult.error("文件上传失败,文件存在");
                } else {
                    Files.write(path, bytes);//创建文件
                    if (true) {
                        securityActivity.setSrId(srId);
                        securityActivity.setAsUrl("profile/srId/" + srId + "/" + file.getOriginalFilename());
                        securityActivityService.insertSecurityActivity(securityActivity);
                    }
                }
            }
            if (null != video) {
                //若依默认系统上传下载路径
                String profile = RuoYiConfig.getProfile();
                byte[] bytes = video.getBytes();
                //文件夹
                Path path2 = Paths.get(profile + "//srId//" + srId);
                //具体文件
                Path path = Paths.get(profile + "//srId//" + srId + "//" + video.getOriginalFilename());
                File file1 = path.toFile();
                //创建文件夹
                if (!file1.exists()) {
                    path2.toFile().mkdirs();
                }
                if (file1.isFile()) {
                    return AjaxResult.error("文件上传失败,文件存在");
                } else {
                    Files.write(path, bytes);//创建文件
                    if (true) {
                        securityActivity.setSrId(srId);
                        securityActivity.setAsUrl("profile/srId/" + srId + "/" + video.getOriginalFilename());
                        securityActivityService.insertSecurityActivity(securityActivity);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return toAjax( securityActivityService.insertSecurityActivity(securityActivity));
    }

    /**
     * 修改安防上报
     */
//    @PreAuthorize("@ss.hasPermi('system:report:edit')")
    @Log(title = "安防上报", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SecurityReport securityReport) {
        return toAjax(securityReportService.updateSecurityReport(securityReport));
    }

    /**
     * 删除安防上报
     */
//    @PreAuthorize("@ss.hasPermi('system:report:remove')")
    @Log(title = "安防上报", businessType = BusinessType.DELETE)
    @DeleteMapping("/{srIds}")
    public AjaxResult remove(@PathVariable Long[] srIds) {
        return toAjax(securityReportService.deleteSecurityReportByIds(srIds));
    }
}
